<?php
	require_once('paths.inc');
	require_once(INCLUDE_PATH . 'constants.inc');
	require_once('page.inc');
	require_once(DB_PATH.'db.inc');
	
	require_once('security.inc');
    require_once('utils.inc');    
    	
	session_start();
	check_permissions(CT_PERM_STATISTICI);
	html_header("LANG_Pagina_Statistici", null, 'statistici');
	
    require_once(INCLUDE_PATH.'constants.inc');

	if (isset($_GET['criteriu'])) $criteriu = $_GET['criteriu']; 
	if (isset($_GET['perioada'])) $perioada = $_GET['perioada']; else $perioada = "";
	if (isset($_GET['tip'])) $tip = $_GET['tip'];	
	
	$date_arr = explode('and', $perioada);
    $linkuri = array(LANG_Home => "home.php", LANG_Pagina_Statistici => "");	
    
    titlu_pagina($linkuri);
	output_notice_msg();  
	output_error_msg();  
	
    output_extra_text(LANG_EXTRA_STATS);	
        
	if (empty($date_arr[1])) $date_arr = array();
    
    $db = db_connect();

    $id_client = $_SESSION['user_logat']->id_client;
    
    try {
    	$firme = Firma::queryLoadByClient($db, $id_client);
    	$arrUsers = User::queryLoadByClient($db, $id_client);
    	$arrTari = Address::queryLoadCountriesByClientForCommands($db, $id_client);
    }
    catch (QueryLoadFirmaException $e) {
    	redirectOnError(CT_STATS_FIRMA_LOAD_BY_CLIENT);
    }
    catch (QueryLoadUserException $e) {
    	redirectOnError(CT_STATS_FIRMA_LOAD_USER);
    }    
    catch (QueryLoadAddressException $e) {
    	redirectOnError(CT_STATS_LOAD_TARI_INCARCARE_DESCARCARE);
    }

    if (isset($_GET['users'])) $users = $_GET['users']; else $users = "-1";
    if (isset($_GET['client'])) $client = $_GET['client']; else $client = "";
    if (isset($_GET['transportator'])) $transportator = $_GET['transportator']; else $transportator = "";
    if (isset($_GET['incarcare'])) $incarcare = $_GET['incarcare']; else $incarcare = "";
    if (isset($_GET['descarcare'])) $descarcare = $_GET['descarcare']; else $descarcare = "";
    
    //data particularizata
    
    if (isset($_GET['data1'])) $data1 = $_GET['data1']; else $data1 = "";
    if (isset($_GET['data2'])) $data2 = $_GET['data2']; else $data2 = "";
        
    if (empty($perioada)) $perioada = $data1 . "and" . $data2;
        
    // postul monedelor
    if (isset($_GET['moneda'])) $moneda = $_GET['moneda'];
    if (empty($moneda)) $moneda = $currencies[$_SESSION['user_logat']->moneda];
    $monede_post = array();
    for ($i = 0; $i < count($monede_simpu); $i++) {
         if (empty($_GET[$monede_simpu[$i]])) $monede_post[$i] = 1;
         else $monede_post[$i] = $_GET[$monede_simpu[$i]];     
    }        
          
?>

<div class="container">
    <form name="statss" id="statss" method="get" action="stats.php">
    
	<table align="left" id="wrapper2">
		<thead>
			<tr>
				<th class="caption" colspan="4"><?php echo LANG_Statistica; ?></th>
			</tr>		
		</thead>
		<tr>
			<td valign="top">
                <fieldset>
                    <legend><?php echo LANG_Parametrii_filtru; ?></legend>
                
				    <table>
                        <tr>
                            <td><?php echo LANG_Criteriu; ?>
                            <td><?php echo LANG_Utilizator; ?>
                            <td><?php echo LANG_Client; ?>
                            <td><?php echo LANG_Transportator; ?>
                            <td><?php echo LANG_Tara_incarcare; ?>
                            <td><?php echo LANG_Tara_descarcare; ?>
                        </tr>
					    <tr>
						    <td>
                                <select name="criteriu">
                                    <option value="profit" <?php if ($criteriu == "profit") echo " selected=\"selected\" "; ?>><?php echo LANG_Profit; ?>
                                    <option value="comenzi" <?php if ($criteriu == "comenzi") echo " selected=\"selected\" "; ?>><?php echo LANG_Comenzi; ?>
                                    <option value="plati" <?php if ($criteriu == "plati") echo " selected=\"selected\" "; ?>><?php echo LANG_Plati; ?>
                                    <option value="incasari" <?php if ($criteriu == "incasari") echo " selected=\"selected\" "; ?>><?php echo LANG_Incasari; ?>                                                                        
                                </select>
                            </td>
                            <td>
                                <select name="users">
                                    <option <?php if ($users == -1) echo " selected=\"selected\""; ?> value="-1">General</option>
                                    <option <?php if ($users == -2) echo " selected=\"selected\""; ?> value="-2"><?php echo LANG_Toti_useri; ?></option>
                                    <?php
                                        for ($i = 0; $i < count($arrUsers); $i++) {
                                            if ($arrUsers[$i]->id_user == $users) $text = " selected=\"selected\" ";
                                            else $text = "";
                                            echo "<option $text value=\"" . $arrUsers[$i]->id_user . "\">" . $arrUsers[$i]->nume . " " . $arrUsers[$i]->prenume . "</option>";                                        
                                        }
                                            
                                    ?>
                                </select>
                            </td>
                            <td>
                                <select name="client" id="cl" onchange="javascript: ver_trans_cl();">
                                    <option value="-1"> - </option>
                                    <?php
                                        for ($i = 0; $i < count($firme); $i++) {
                                            if ($firme[$i]->id_firma == $client) $text = " selected=\"selected\" ";
                                            else $text = "";                                            
                                            echo "<option $text value=\"" . $firme[$i]->id_firma . "\">" . $firme[$i]->nume . "</option>";                                        
                                        }                                            
                                    ?>
                                </select>
                            </td>
                            <td>
                                <select name="transportator" id="tr" onchange="javascript: ver_trans_cl();">
                                    <option value="-1"> - </option>
                                    <?php
                                        for ($i = 0; $i < count($firme); $i++) {
                                            if ($firme[$i]->id_firma == $transportator) $text = " selected=\"selected\" ";
                                            else $text = "";                                            
                                            echo "<option $text value=\"" . $firme[$i]->id_firma . "\">" . $firme[$i]->nume . "</option>";                                        
                                        }                                            
                                    ?>                                
                                </select>
                            </td>
                            <td>
                                <select name="incarcare">
                                    <option value="-1"> - </option>
                                    <?php
                                        for ($i = 0; $i < count($arrTari); $i++) {
                                            if ($arrTari[$i] == $incarcare) $text = " selected=\"selected\" ";
                                            else $text = "";
                                            echo "<option $text value=\"" . $arrTari[$i] . "\">" . $arrTari[$i] . "</option>";                                        
                                        }                                            
                                    ?>                                   
                                </select>
                            </td>
                            <td>
                                <select name="descarcare">
                                    <option value="-1"> - </option>
                                    <?php
                                        for ($i = 0; $i < count($arrTari); $i++) {
                                            if ($arrTari[$i] == $descarcare) $text = " selected=\"selected\" ";
                                            else $text = "";                                            
                                            echo "<option $text value=\"" . $arrTari[$i] . "\">" . $arrTari[$i] . "</option>";                                        
                                        }                                            
                                    ?>                                     
                                </select>
                            </td>                                                        
					     </tr>
                    </table>
                </fieldset>
                <fieldset>
                    <legend><?php echo LANG_Perioada; ?></legend>
      	                <input onclick="javascript: curata();" <?php if ($perioada == 'luna_curenta') echo "checked=\"checked\""; ?> type="radio" name="perioada" value="luna_curenta" /><?php echo LANG_Luna_curenta; ?>
      	                <input onclick="javascript: curata();" <?php if ($perioada == '3_luni') echo "checked=\"checked\""; ?> type="radio" name="perioada" value="3_luni" /> <?php echo LANG_Ultimele_3_luni; ?>
      	                <input onclick="javascript: curata();" <?php if ($perioada == '6_luni') echo "checked=\"checked\""; ?> type="radio" name="perioada" value="6_luni" /> <?php echo LANG_Ultimele_6_luni; ?>
      	                <input onclick="javascript: curata();" <?php if ($perioada == '1_an') echo "checked=\"checked\""; ?> type="radio" name="perioada" value="1_an" /> <?php echo LANG_Ultimele_12_luni; ?>
      	                <input onclick="javascript: curata();" <?php if ($perioada == 'tot') echo "checked=\"checked\""; ?> type="radio" name="perioada" value="tot" /><?php echo LANG_Toata_perioada; ?>                          		            

                        <input <?php if (strlen($perioada) > 12) echo " checked=\"checked\" "; ?> onclick="javascript: dezgheata();" type="radio" name="perioada" value="" />
      		            <?php                              
                            echo LANG_Chose_custom_date. " "; 
                            $date_arr = explode('and', $perioada);
                            if (count($date_arr) == 1) {
                            	$date_arr = 1;
                            	$d1 = "";
                            	$d2 = "";
                            }
                            else {
                                $d1 = $date_arr[0];
                                $d1_arr = explode("-", $date_arr[0]);
                                $d1 = $d1_arr[2] . "-" . $d1_arr[1] . "-" . $d1_arr[0];
                                
                                $d2 = $date_arr[1];
                                $d2_arr = explode("-", $date_arr[1]);
                                $d2 = $d1_arr[2] . "-" . $d2_arr[1] . "-" . $d2_arr[0];
                                $date_arr = 0;                                
                            }
                        echo LANG_intre . " ";
                        echo render_date_field_button($d1, 'data1', 'data_scadenta_exec2', '', $date_arr, 'image1', 'cal1', 1); 
                        echo " " . LANG_si . " ";
                        echo render_date_field_button($d2, 'data2', 'data_scadenta_exec2', '', $date_arr, 'image2', 'cal2', 1); ?>
                </fieldset>
                <fieldset>
                    <legend><?php echo LANG_Options; ?></legend>
                    	<?php echo LANG_selectati_tipul_graficului; ?>
                        <select name="tip">
                            <option <?php if ($tip == "Line") echo " selected=\"selected\" "; ?> value="Line"><?php echo LANG_Line; ?>
                            <option <?php if ($tip == "stacked_3d") echo " selected=\"selected\" "; ?> value="stacked_3d"><?php echo LANG_stacked_3d_column; ?>
                        </select> 
                        <?php echo LANG_selectati_moneda; ?>
                        <select name="moneda" onchange="javascript: schimba_monede(this.value);">
                        	<?php
                        	     for ($k = 0; $k < count($monede_simpu); $k++) {
                        	          if ($moneda == $monede_simpu[$k]) $text = " selected = \"selected\" ";
                        	          else $text = "";                         	          
                        	          echo '<option '. $text . ' value="' . $monede_simpu[$k] . '">' . $monede_simpu[$k] . '</option>';          
                        	     }                        	
                        	?>                           
                        </select>
                        <span id="tip_moneda">
                        	<?php
                        	  $tx = "";
                              for ($k = 0; $k < count($monede_simpu); $k++) {
                                   if ($monede_simpu[$k] != $moneda)
                                        $tx .= '1 '. $moneda . ' = <input type="text" size="8" id="' . $monede_simpu[$k] . '" value="' . $monede_post[$k] . '" name="' . $monede_simpu[$k] . '" /> ' . $monede_simpu[$k] . ', ';      
                              }                                      
                              echo substr($tx, 0, strlen($tx) - 2);       
      	
                        	?>                        	                        
                        </span>
                        <input onclick="javascript: show_stats();" id="buton_custom" type="button" value="<?php echo LANG_Show_statistica; ?>" />                                                
                </fieldset>
		<div class="flashobj">
			<OBJECT classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"
				codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0" 
				WIDTH="900" 
				HEIGHT="500" 
				id="charts" 
				ALIGN="">
				<PARAM NAME=movie VALUE="include/charts/charts.swf?library_path=include/charts/charts_library&xml_source=generate_chart.php%3Fcriteriu%3D<?php echo $criteriu; ?>%26perioada=<?php echo $perioada; ?>%26tip=<?php echo $tip; ?>">
				<PARAM NAME=quality VALUE=high>
				<PARAM NAME=bgcolor VALUE=#666666>
				<param name="wmode" value="transparent">
				<EMBED src="include/charts/charts.swf?library_path=include/charts/charts_library&xml_source=generate_chart.php%3Fcriteriu%3D<?php echo $criteriu; ?>%26perioada=<?php echo $perioada; ?>%26tip=<?php echo $tip; ?>%26user=<?php echo $users; ?>%26client=<?php echo $client; ?>%26trans=<?php echo $transportator; ?>%26incarcare=<?php echo $incarcare; ?>%26descarcare=<?php echo $descarcare; ?>%26moneda=<?php echo $moneda; ?><?php for ($i = 0; $i < count($monede_simpu); $i++) echo "%26{$monede_simpu[$i]}={$monede_post[$i]}"; ?>" 
					   quality=high 
					   bgcolor=#FFFFFF  
					   WIDTH="900" 
					   HEIGHT="500" 
					   NAME="charts" 
					   ALIGN="" 
					   swLiveConnect="true" 
					   wmode="transparent"
					   TYPE="application/x-shockwave-flash" 
					   PLUGINSPAGE="http://www.macromedia.com/go/getflashplayer">
				</EMBED>
			</OBJECT>
		</div>
	</td>
		</tr>
	</table>
    </form>
</div>    	
<?php html_footer(); ?>	